<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>

<SCRIPT language="JavaScript" src="/timereport/include/common.js"></SCRIPT>

<!-- Jquery per date picker  -->
   <link rel="stylesheet" href="/timereport/include/jquery/jquery-ui-1.10.3.custom.min.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script type="text/javascript" src="/timereport/include/jquery/jquery.ui.datepicker-it.js"></script>
  <script src="/timereport/include/jquery/jquery-ui-1.10.3.custom.min.js"></script>    
  <script>
      $(function () {
          $("#datepicker").datepicker($.datepicker.regional['it']);
      });
  </script>
<!-- FINE Jquery per date picker  -->

<SCRIPT language="JavaScript">
function validate() {

// Verifica che tutti i campi obbligatori siano stati compilati
	if ( ForceEntry(document.form1.amount,"Valore") == false) 
		return false;

// Verifica che l'input sia numerico
	if ( !IsNumeric(document.form1.amount) ) 
		return false; 

//	Verifica il formato del campo, al massimo due interi e due decimali		
  	if ( !CheckDecimals(document.form1.amount,9,2) )
  	    return false;

	return true;
}
</SCRIPT>
<%

	Dim objConn1, objRs1
	Dim strInputDisabled 	' used for disable the input field
	Dim strCssInput		' used for setting the input attribute
    Dim objConnDt, objRsDt        
	
	strCSSInput = "FormInput" 	' is changed in case of updating not allowed
	
'   --- if user selected the cancel button go back to main screen ---
	if request.form("cancel") <> "" then
			response.redirect("input.asp")
	End If 
'	-----------------------------------	


'	----------------------------	
'	Values of action:
'	new:	called by input.asp by clicking on "new item" icon
'	fetch:	called by input.asp by clicking on existing item
'	add:	called by itself for insert, insert and add, insert and increment date buttons
'	update: called by itself for update button

	Select case request("action")

	case "fetch"
		call MakeConn(objConn, DATABASE)	
		call MakeRs_view(objRs,objConn,"SELECT * FROM Expenses  where expenses_id =" & request("expenses_id") )						

'       Autore e data creazione   
        Dim strCreationData
        
        If objRs("CreatedBy") <> "" then
            call MakeConn(objConnDt, DATABASE)	
	        call MakeRs_view(objRsDt,objConnDt,"SELECT Name FROM Persons where persons_id =" & objRs("CreatedBy") )				
            strCreationData = "<a href='AuditLog.aspx?RecordId=" & request("expenses_id") & "&TYPE=I&TableName=Expenses&key=<Expenses_Id=" & request("expenses_id") & ">'>" & _
                              objRsDt("Name") & " " & formatDateTime( objRS("CreationDate"), vbShortDate )  & "  " & formatDateTime( objRS("CreationDate"), vbShortTime )  & "</a>"    
            destroy(objRsDt)
	        destroy(objConnDt)
        End if

'       Autore e data modifica
        Dim strModificationData
        
        If objRs("LastModifiedBy") <> "" then
            call MakeConn(objConnDt, DATABASE)	
	        call MakeRs_view(objRsDt,objConnDt,"SELECT Name FROM Persons where persons_id =" & objRs("LastModifiedBy") )				
            strModificationData =  "<a href='AuditLog.aspx?RecordId=" & request("expenses_id") & "&TYPE=U&TableName=Expenses&key=<Expenses_Id=" & request("expenses_id") & ">'>" & _
                                    objRsDt("Name") & " " & formatDateTime( objRS("LastModificationDate"), vbShortDate)  & "  " & formatDateTime( objRS("LastModificationDate"), vbShortTime )  & "</a>"  
            destroy(objRsDt)   
	        destroy(objConnDt)
        End if

	case "add"
		call MakeConn(objConn, DATABASE)	
		call MakeRs_add(objRs,objConn,"expenses" )	
		objRs.addnew
		objRs("Date")  = request("date")
		' set values
		objRs("Projects_Id") = request("Projects_id") 			
		objRs("Persons_Id") = session("persons_id") 
		objRs("ExpenseType_Id") = request("ExpenseType_id") 
		objRs("amount") = iif(request("CancelFlag")="X",-1,1) * request("amount") 
		objRs("comment") = request("comment")						
		If request("CreditCardPayed")  = "X" Then 
			objRs("CreditCardPayed") = true 
		Else	
			objRs("CreditCardPayed") = false
		End If
		If request("CancelFlag")  = "X" Then 
			objRs("CancelFlag") = true 
		Else	
			objRs("CancelFlag") = false
		End If
		'InvoiceFlag management
		If request("InvoiceFlag")  = "X" Then 
			objRs("InvoiceFlag") = true 
		Else	
			objRs("InvoiceFlag") = false
		End If		
        ' Audit
        objRs("CreatedBy") =  session("persons_id") 
        objRs("CreationDate") = now()
        objRs("AccountingDate") = request("AccountingDate")

		objRs.update		
		'Clear open connections	
		destroy(objRs)
		destroy(objConn)			
		' save the project on a session variable to give a default on the next entry
		session("ProjectCodeDefault") = request("Projects_id") 
		session("ExpenseTypeDefault") = request("ExpenseType_id") 	        	

	case "update"
		call MakeConn(objConn, DATABASE)	
		call MakeRs_add(objRs,objConn,"SELECT * FROM Expenses  where expenses_id =" & request("expenses_id") )						
		' set values
		objRs("Projects_Id") = request("Projects_id") 			
		objRs("Persons_Id") = session("persons_id") 
		objRs("ExpenseType_Id") = request("ExpenseType_id") 
		objRs("amount") = iif(request("CancelFlag")="X",-1,1) * request("amount") 
		objRs("comment") = request("comment")		
        				
		If request("CreditCardPayed")  = "X" Then 
			objRs("CreditCardPayed") = true 
		Else	
			objRs("CreditCardPayed") = false
		End If
		If request("CancelFlag")  = "X" Then 
			objRs("CancelFlag") = true 
		Else	
			objRs("CancelFlag") = false
		End If
		'InvoiceFlag management
		If request("InvoiceFlag")  = "X" Then 
			objRs("InvoiceFlag") = true 
		Else	
			objRs("InvoiceFlag") = false
		End If	
        ' Audit
        objRs("LastModifiedBy") =  session("persons_id") 
        objRs("LastModificationDate") = now()
        objRs("AccountingDate") = request("AccountingDate")

		objRs.update		
		'Clear open connections	
		destroy(objRs)
		destroy(objConn)			
		' save the project on a session variable to give a default on the next entry
		session("ProjectCodeDefault") = request("Projects_id") 
		session("ExpenseTypeDefault") = request("ExpenseType_id") 		
		' go back to main screen
		response.redirect("input.asp")		
	
	End Select		

'	--- If "add another" function was not selected then go back to main screen and it's not the first time
	If request("action") = "add" and request.form("insert+") = "" and request.form("insert+data") = "" Then
		response.redirect("input.asp")		
	End If

'	---------------------------------			
	If request.form("insert+data") <> "" Then	
		strDate = dateadd( "d", 1, request("date"))
	else
		strDate = request("date")
	End if								


	'------------------------------------------------------
	Function SetDefault( strFieldName )
		
		if request("action") = "fetch" Then ' called in update mode	
			if isnumeric (objRs(strFieldName)) Then
				SetDefault =  abs (objRs(strFieldName))
			Else
				SetDefault =  objRs(strFieldName) 
			End if
		Else
			SetDefault = ""
		End If
			
	End Function

	'------------------------------------------------------
	Function SetDefaultDate( strFieldName )
		
		if request("action") = "fetch" Then ' called in update mode	
			if len(objRS(strFieldName)) > 0 Then
				SetDefaultDate = formatDateTime( objRS(strFieldName), vbShortDate  )  
			Else
				SetDefaultDate = ""
            End if
        End if              
			
	End Function

%>
<html>
<!-- InstanceBegin template="/Templates/common.dwt" codeOutsideHTMLIsLocked="false" -->
<!--#include virtual="/timereport/include/auth.asp" -->
<!--#include virtual="/timereport/include/common.asp" -->
<head>
<!-- InstanceBeginEditable name="doctitle" -->
<title>Input spese</title>
<!-- InstanceEndEditable -->
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!-- InstanceBeginEditable name="head" -->
<link href="include/commoncss.asp" rel="stylesheet" type="text/css">
<!-- InstanceEndEditable -->
</head>
<SCRIPT language=JavaScript src= "/timereport/include/menu/menu_array.js" type=text/javascript></SCRIPT>
<SCRIPT language=JavaScript src= "/timereport/include/menu/mmenu.js" type=text/javascript></SCRIPT>
<body>
<table width="760" border="0" align="left" cellpadding="0" cellspacing="0">
  <tr>
    <td height="30" class="BarraTop"><div align="right">Utente: <%= session("UserName") %> </div></td>
  </tr>
  <tr>
    <td height="30">&nbsp;</td>
  </tr>
  <tr> <!-- InstanceBeginEditable name="principale" -->
    <td ><form name="form1" method="post" action="input-spese.asp">
        <table width="60%" border="0" align="center" cellpadding="0" cellspacing="0" class="TabellaForm">
          <tr>
            <th width="20%" valign="top">Data</th>
            <td valign="top"><%
	if request("action") = "fetch" Then ' called in update mode
		response.write( FormatDateTime(objRS("date") , 1) )
		
		' if input is disabled the set the attribute of the form input and make them disabled		
		if session("CutoffDate") > objRS("date") Then
			strInputDisabled="Disabled"
			strCssInput = "FormInputDisabled"
		else
			strInputDisabled = ""
		End If
		
	else
		response.write( FormatDateTime(strDate , 1) )
	End If 
%>
              <input type="hidden" name="date"	 value="<%=strDate %>">
            </td>
          </tr>
          <tr>
            <th valign="top">Persona</th>
            <td valign="top"><%		
				response.write( Session("UserName") )
				%>
            </td>
          </tr>
          <tr>
            <th valign="top">Progetto</th>
            <td valign="top"><select name="Projects_Id" class="<%=strCSSInput%>" <%=strInputDisabled%>  >
                <%		
            		Dim aProject, i
'					importa la lista dei progetti ammessi	
					aProject = Session("ProgettiForzati") 
																	
            		For i=0 to Ubound(aProject, 2)
            			if request("action") = "fetch" then	' update record
            				If  aProject(0,i) = objRs("Projects_id") Then
            					response.write("<option value=" & aProject(0,i) & " selected >" &aProject(1,i) & " : " & aProject(2,i) & "</option>")            			
            				Else
            					response.write("<option value=" & aProject(0,i) & ">" &aProject(1,i) & " : " & aProject(2,i) & "</option>")            		            					
            				End If
						Else
            				If  aProject(0,i) = CInt(session("ProjectCodeDefault")) Then						
            					response.write("<option value=" & aProject(0,i) & " selected >" &aProject(1,i) & " : " & aProject(2,i) & "</option>")            		
							Else
            					response.write("<option value=" & aProject(0,i) & ">" &aProject(1,i) & " : " & aProject(2,i) & "</option>")            									
							End If
            			End If
            		Next            		            		
%>
              </select>
            </td>
          </tr>
          <tr>
            <th valign="top">Tipo</th>
            <td valign="top"><select name="ExpenseType_id" class="<%=strCSSInput%>" <%=strInputDisabled%>  >
                <%            		
            		Dim aExpenses
            		aExpenses = session("SpeseForzate")            		
            	
            		For i=0 to Ubound(aExpenses, 2)
            			if request("action") = "fetch" then	' update record
            				If  aExpenses(0,i) = objRs("ExpenseType_id") Then
            					response.write("<option value=" & aExpenses(0,i) & " selected >" &aExpenses(1,i) & " : " & aExpenses(2,i) & "</option>")            			
            				Else
            					response.write("<option value=" & aExpenses(0,i) & ">" &aExpenses(1,i) & " : " & aExpenses(2,i) & "</option>")            		            					
            				End If
						Else
							If aExpenses(0,i) = CInt(session("ExpenseTypeDefault")) Then
            					response.write("<option value=" & aExpenses(0,i) & " selected >" &aExpenses(1,i) & " : " & aExpenses(2,i) & "</option>")            		
							Else
            					response.write("<option value=" & aExpenses(0,i) & ">" &aExpenses(1,i) & " : " & aExpenses(2,i) & "</option>")            									
							End If            					
            			End If
            		Next            		            		
%>
              </select></td>
          </tr>
          <tr>
            <th valign="top">Valore / km</th>
            <td valign="top"><input name="amount" type="text" class="<%=strCSSInput%>" <%=strInputDisabled%>  value="<%=SetDefault("amount")%>" >
            </td>
          </tr>
          <tr>
            <th valign="top" nowrap>Pag. Azienda</th>
            <td valign="top"><input name="CreditCardPayed" <%=strInputDisabled%> type="checkbox" value="X" 
<%
	If request("action") = "fetch" Then
		If objRs("CreditCardPayed") = True Then ' called in update mode
			response.write(" checked ")
		End If
	End If
%>  ></td>
          </tr>
          <tr>
            <th valign="top" nowrap>Storno</th>
            <td valign="top"><input name="CancelFlag" <%=strInputDisabled%> type="checkbox" value="X" 
<%
	If request("action") = "fetch" Then
		If objRs("CancelFlag") = True Then ' called in update mode
			response.write(" checked ")
		End If
	End If
	%>  ></td>
          </tr>
                    <tr>
            <th valign="top" nowrap>Fattura</th>
            <td valign="top"><input name="InvoiceFlag" <%=strInputDisabled%> type="checkbox" value="X" 
<%
	If request("action") = "fetch" Then
		If objRs("InvoiceFlag") = True Then ' called in update mode
			response.write(" checked ")
		End If
	End If
	%>  ></td>
          </tr>
          <tr>
            <th valign="top">Nota</th>
            <td valign="top"><textarea name="comment" cols="50" rows="5"  class="<%=strCSSInput%>" <%=strInputDisabled%> ><%=SetDefault("comment")%></textarea></td>
          </tr>
          
          <%    ' check se deve visualizzare data contabile
                if session("userLevel") >= 4 then %>

          <tr>
            <th valign="top">Data competenza</th>
            <td valign="top"><input name="AccountingDate" type="text" id="datepicker" class="<%=strCSSInput%>" <%=strInputDisabled%>  value="<%=SetDefaultDate("AccountingDate") %>" >
            </td>
          </tr>

          <%  End If ' Solo se autorizzazioni per visualizzare data contabile %>

          <%	if request("action") = "fetch" then %>
          <tr>
            <th valign="top">Creato</th>
            <td valign="top"><%=strCreationData%></td>
          </tr>
          <tr>
            <th valign="top">Modificato</th>
            <td valign="top"><%=strModificationData%></td>
          </tr>
          <% End if %>
        </table>
        <br>
        <table width="60%" border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td width="50%"><%	if request("action") = "fetch" then %>
              <input type="submit" name="update" value="aggiorna" class="FormButtont" onClick="return validate()">
              &nbsp;
              <Input type="hidden" name="action" value="update">
              <input type="hidden" name="expenses_id" value="<%=objRs("Expenses_id")%>">
              <% else %>
              <input type="submit" name="insert" value="salva" class="FormButtont" onClick="return validate()">
              &nbsp;
              <input type="submit" name="insert+" value="salva +spesa" class="FormButtont" onClick="return validate()">
              &nbsp;
              <input type="submit" name="insert+data" value="salva +data" class="FormButtont" onClick="return validate()">
              &nbsp;
              <input type="hidden" name="action" value="add">
              <% End if %>
              <input type="submit" name="cancel" value=" annulla " class="FormButtont">
            </td>
          </tr>
        </table>
      </form></td>
    <%
			
	if request("action") = "fetch" Then	' called in update mode
	   	destroy(objRs)
		destroy(objConn)				
	End If
%>
    <!-- InstanceEndEditable --></tr>
</table>
</body>
<!-- InstanceEnd -->
</html>
